﻿using System;
using System.Text;

namespace Useful.Document
{
    public static class ExcelHelper
    {
        public static int GetDataFoInsert(string str, out object[,] data)
        {
            string[] strings = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            data = new object[strings.Length, 26];

            for (int i = 0; i < strings.Length; i++)
            {
                if (!String.IsNullOrEmpty(strings[i]))
                {
                    string[] items = strings[i].Split(new char[] { '\t' });

                    for (int j = 0; j < items.Length; j++)
                    {
                        data[i, j] = items[j];
                    }
                }
            }

            return strings.Length;
        }

        public static string GetTextFromObjects(object objects)
        {
            object[,] array = (object[,])objects;

            StringBuilder sb = new StringBuilder();

            for (int i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
            {
                for (int j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
                {
                    object cell = array[i, j];

                    if (cell != null)
                        sb.Append(cell.ToString() + " ");
                }
            }

            return sb.ToString();
        }
    }
}
